<?php
class UserController extends Bib_Controller_Action{
    public function indexAction(){
        $users = UserManager::getInstance()->getNormalUsers();
        $this->view->users = $users;
//        var_dump($users);
//        die;
    }

    public function addUserAction(){
        $form = new Form_User_AddUser();

		if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
			$user = new User();
			$user->haslo = sha1($form->getValue('password'));
            $user->email = $form->getValue('mail');
            $user->aktywny = 1;
            $user->dataUtworzenia = date('Y-m-d H:i:s');
			UserManager::getInstance()->save($user);

			$this->view->infoBox();
			$this->view->message = "Użytkowik został dodany.";
			$this->view->redirect = array('controller' => 'user');
		}

		$this->view->form = $form;
    }

    public function editUserAction(){
        $mail = $this->_request->getParam('mail');        
        $form = new Form_User_ChangePassword();
        $form->getElement('mail')->setValue($mail);

		if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
			$user = UserManager::getInstance()->getByEmail($mail);


            $user->aktywny = 1;
			$user->haslo = sha1($form->getValue('password'));
			UserManager::getInstance()->save($user);

			$this->view->infoBox();
			$this->view->message = "Hasło zmienione.";
			$this->view->redirect = array('controller' => 'user');
		}

		$this->view->form = $form;
    }

    public function deleteUserAction(){
        $mail = $this->_request->getParam('mail');
        $user = UserManager::getInstance()->getByEmail($mail);
        UserManager::getInstance()->delete($user);

        $this->view->infoBox();
        $this->view->message = "Użytkownik usunięty.";
        $this->view->redirect = array('controller' => 'user');
    }
}
